﻿/**
 * 判断是否是 Map 类型
 */
export function isMap(obj: any): obj is Map<any, any> {
  return obj instanceof Map;
}

/**
 * 获取构造函数的参数类型
 */
export type ConstructorParameters<T extends new (...args: any[]) => any> =
  T extends new (...args: infer P) => any ? P : never;

/**
 * 获取函数的参数类型
 */
export type Parameters<T> = T extends (...args: infer P) => any ? P : never;

/**
 * 获取构造函数的实例类型
 */
export type InstanceType<T extends new (...args: any[]) => any> =
  T extends new (...args: any[]) => infer R ? R : any;

/**
 * 获取数组最后一个元素
 */

export type Last<T extends any[]> = T extends [...infer _, infer Last]
  ? Last
  : undefined;

export function lastElement<T extends any[]>(arr: T): Last<T> {
  return (arr.length > 0 ? arr[arr.length - 1] : undefined) as Last<T>;
}
